<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Billstream extends CI_Controller {

    /**
     * Index
     */
    public function index() {

        $data['message'] = '';

        if ($this->input->get('message')) {
            $data['message'] = base64_decode($this->input->get('message'));
        }

        // Load Biller Class
        $this->load->model('biller');

        // Get a list of Bills
        $data['bills'] = $this->biller->get_bills();

        // Load the List Bills View
        $this->load->view('list_bills', $data);
    }

    /**
     * Add
     */
    public function add() {

        $data['mode'] = 'add';

        $data['id'] = 0;
        $data['name'] = '';
        $data['amount'] = '';
        $data['freq'] = 'M';
        $data['byday'] = 'SU';
        $data['bymonth'] = 1;
        $data['bymonthday'] = 1;

        // If POST with "save" click
        if ($this->input->post('save')) {
            // Save Biller
            // Collect Data
            $data['name'] = $this->input->post('name');
            $data['amount'] = $this->input->post('amount');
            $data['freq'] = $this->input->post('freq');
            $data['byday'] = $this->input->post('byday');
            $data['bymonth'] = $this->input->post('bymonth');
            $data['bymonthday'] = $this->input->post('bymonthday');

            // Populate Model
            $this->load->model('biller');
            $this->biller->name = $data['name'];
            $this->biller->amount = $data['amount'];


            // Convert input to RRule
            $this->load->library('rrule');
            $this->biller->rrule = $this->rrule->inputs_to_rrule(
                    $data['freq'], $data['byday'], $data['bymonth'], $data['bymonthday']
            );

            // Validation Rules
            $this->form_validation->set_rules(
                    'name', 'Name', 'required|min_length[1]|max_length[25]'
            );
            $this->form_validation->set_rules(
                    'amount', 'Amount', 'required|min_length[1]|max_length[8]|is_numeric'
            );


            // Validate
            if ($this->form_validation->run() == FALSE) {

                // Validation failed
                // Load the View
                $this->load->view('edit_biller', $data);
            } else {

                // Validation good, insert record
                $this->biller->insert();

                // Done, redirect to home
                redirect(site_url() . '?message=' . base64_encode($this->biller->name . ' has been added'));
            }
        } else if ($this->input->post('cancel')) {

            //Cancel Biller
            redirect(site_url() . '?message=' . base64_encode('Add Biller has been cancelled'));
        } else {
            // GETed so show add form
            // Show Add form
            $this->load->view('edit_biller', $data);
        }
    }

    /**
     * Edit
     */
    public function edit() {

        $data['mode'] = 'edit';

        $data['id'] = 0;
        $data['name'] = '';
        $data['amount'] = '';
        $data['freq'] = 'M';
        $data['byday'] = 'SU';
        $data['bymonth'] = 1;
        $data['bymonthday'] = 1;

        // If POST with "save" click
        if ($this->input->post('save')) {
            // Save Biller
            // Collect Data
            $data['id'] = $this->input->post('id');
            $data['name'] = $this->input->post('name');
            $data['amount'] = $this->input->post('amount');
            $data['freq'] = $this->input->post('freq');
            $data['byday'] = $this->input->post('byday');
            $data['bymonth'] = $this->input->post('bymonth');
            $data['bymonthday'] = $this->input->post('bymonthday');

            // Populate Model
            $this->load->model('biller');
            $this->biller->id = $data['id'];
            $this->biller->name = $data['name'];
            $this->biller->amount = $data['amount'];


            // Convert input to RRule
            $this->load->library('rrule');
            $this->biller->rrule = $this->rrule->inputs_to_rrule(
                    $data['freq'], $data['byday'], $data['bymonth'], $data['bymonthday']
            );

            // Validation Rules
            $this->form_validation->set_rules(
                    'name', 'Name', 'required|min_length[1]|max_length[25]'
            );
            $this->form_validation->set_rules(
                    'amount', 'Amount', 'required|min_length[1]|max_length[8]|is_numeric'
            );


            // Validate
            if ($this->form_validation->run() == FALSE) {

                // Validation failed
                // Load the View
                $this->load->view('edit_biller', $data);
            } else {

                // Validation good, insert record
                $this->biller->update();

                // Done, redirect to home
                redirect(site_url() . '?message=' . base64_encode($this->biller->name . ' has been modified'));
            }
        } else if ($this->input->post('cancel')) {

            //Cancel Biller
            redirect(site_url() . '?message=' . base64_encode('Add Biller has been cancelled'));
        } else if ($this->input->get('id')) {

            //We have an ID
            $data['id'] = $this->input->get('id');

            // Lookup Biller
            $this->load->model('biller');
            $this->biller->get($data['id']);

            // Set name and amount
            $data['name'] = $this->biller->name;
            $data['amount'] = $this->biller->amount;

            // convert rrule to inputs
            $this->load->library('rrule');
            foreach ($this->rrule->rrule_to_inputs($this->biller->rrule) as $key => $value) {
                $data[$key] = $value;
            }

            // Show Add form
            $this->load->view('edit_biller', $data);
        } else {
            // No ID so redirect to home page
            redirect(site_url());
        }
    }

    /**
     * Delete
     */
    public function delete() {

        // If POST with "save" click
        if ($this->input->post('confirm')) {

            $this->load->model('biller');
            $this->biller->id = $this->input->post('id');

            if ($this->biller->delete()) {
                // Success
                redirect(site_url() . '?message=' . base64_encode('Biller has been deleted'));
            } else {

                // Fail
                $data['message'] = 'Could not delete this biller';

                $this->load->view('delete_biller', $data);
            }
        } else if ($this->input->post('cancel')) {

            //Cancel Biller
            redirect(site_url() . '?message=' . base64_encode('Delete Biller has been cancelled'));
        } else if ($this->input->get('id')) {

            //We have an ID
            $data['id'] = $this->input->get('id');

            // Lookup Biller
            $this->load->model('biller');
            $this->biller->get($data['id']);

            // Set name and amount
            $data['name'] = $this->biller->name;
            $data['amount'] = $this->biller->amount;

            // Load the View
            $this->load->view('delete_biller', $data);
        } else {

            // Not posted and no ID so redirect to the home page
            redirect(site_url());
        }
    }

}

/* End billstream.php */
